package com.woniuxy.demo;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @Author: 马宇航
 * @Description: P生产者
 * @DateTime: 22/10/19/0019 14:19
 * @Component: 成都蜗牛学苑
 **/
public class Producer {
    public static final String QUEUE_NAME="myFirstQueue";
    public static final String EXCHANGE_NAME="myFirstExchange";
    public static void main(String[] args) throws IOException, TimeoutException {
        //定义连接和信道
        ConnectionFactory connectionFactory = new ConnectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        Channel channel1 = connection.createChannel();
        //信道与队列绑定
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //定义交换机 没有routingkey
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT.getType(),false,false,null);
        //绑定交换机与队列，并配置上 routingkey：channel与交换机
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"DIRECT_binding");
        String message = "hello my 91 class";
        //通过信道发布消息，发布到下面的（其实有一个空串交换机）队列中
        channel.basicPublish(EXCHANGE_NAME,"DIRECT_binding",null,message.getBytes(StandardCharsets.UTF_8));
        System.out.println("消息发送完成。");

        //channel关闭
        channel.close();
        //connection关闭
        connection.close();
    }
}
